package com.atlansheng.rocketmqdemoboot.demos.service;

import com.atlansheng.rocketmqdemoboot.demos.conf.SpringUtil;
import com.atlansheng.rocketmqdemoboot.demos.po.OneJifen;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: JiFenServiceImpl
 * @Description:
 * @Date 2024/03/04 16:23
 * @Author songshaoqiu
 **/
@Service
@RequiredArgsConstructor
public class JiFenServiceImpl extends ServiceImpl<JiFenDao, OneJifen> implements JiFenService {

    private final JiFenDao jiFenDao;
    //学习一下本地事务的传播性的

    @Override
    @Transactional
    public void doLocalTrans() {
        OneJifen oneJifen = new OneJifen();
        oneJifen.setJiFen("1000");
        oneJifen.setOrderNo("MainOrder1401");
        jiFenDao.insert(oneJifen);

        //同一个类方法里面直接调用，会发现B事务失效了。
        // ((JiFenServiceImpl) AopContext.currentProxy()).doLocalTransB();
        //但是C事务没有影响。
        JiFenServiceImpl bean = SpringUtil.getBean(JiFenServiceImpl.class);
        bean.doLocalTransc();
        int i =1/0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void doLocalTransB(){
        OneJifen oneJifen = new OneJifen();
        oneJifen.setJiFen("1000");
        oneJifen.setOrderNo("BBBBBOrder1402");
        JiFenServiceImpl bean = SpringUtil.getBean(JiFenServiceImpl.class);
        bean.save(oneJifen);
    }
    //如果主方法的没有加事务。
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void doLocalTransc(){
        OneJifen oneJifen = new OneJifen();
        oneJifen.setJiFen("1000");
        oneJifen.setOrderNo("CCCCOrder1402");
        jiFenDao.insert(oneJifen);
    }
}
